package com.tom;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;

/**
 * Servlet implementation class RegServlet
 */
public class RegServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if (action.equalsIgnoreCase("register")){
		String id=null;
		String password1=null;
		String password2=null;
		String name_c=null;
		String name_a=null;
		String brith=null;
		String tel_area1=null;
		String telephone1=null;
		String telephone1_p=null;
		String mobile1=null;
		String tel_area2=null;
		String telephone2=null;
		String telephone2_p=null;
		String mobile2=null;
		String address1=null;
		String address2=null;
		String original=null;
		String professional=null;
		String company_num=null;
		String company_name=null;
		String company_job=null;
		String company_tel_area=null;
		String company_tel=null;
		String company_tel_p=null;
		String contact_name=null;
		String contact_keep=null;
		String contact_area=null;
		String contact_tel=null;
		String contact_tel_p=null;
		String email=null;
		
		String answer = request.getParameter("answer");
        request.getSession().removeAttribute("CAP_WRONG");
        Captcha cap = (Captcha)request.getSession().getAttribute(Captcha.NAME);
        if (!cap.isCorrect(answer)){
                request.getSession().setAttribute("CAP_WRONG", "驗證碼錯誤");
                RequestDispatcher disp = request.getRequestDispatcher("reg.jsp");
                disp.forward(request, response);
                return;
        }
		
		
		if (request.getParameter("id") != null) {
			id = request.getParameter("id");
			password1 = request.getParameter("password1");
			password2 = request.getParameter("password2");
			name_c = request.getParameter("name_c");
			name_a = request.getParameter("name_a");
			brith = request.getParameter("brith");
			tel_area1 = request.getParameter("tel_area1");
			telephone1 = request.getParameter("telephone1");
			telephone1_p = request.getParameter("telephone1_p");
			mobile1 = request.getParameter("mobile1");
			tel_area2 = request.getParameter("tel_area2");
			telephone2 = request.getParameter("telephone2");
			telephone2_p = request.getParameter("telephone2_p");
			mobile2 = request.getParameter("mobile2");
			address1 = request.getParameter("address1");
			address2 = request.getParameter("address2");
			original = request.getParameter("original");
			professional = request.getParameter("professional");
			company_num = request.getParameter("company_num");
			company_name = request.getParameter("company_name");
			company_job = request.getParameter("company_job");
			company_tel_area = request.getParameter("company_tel_area");
			company_tel = request.getParameter("company_tel");
			company_tel_p = request.getParameter("company_tel_p");
			contact_name = request.getParameter("contact_name");
			contact_keep = request.getParameter("contact_keep");
			contact_area = request.getParameter("contact_area");
			contact_tel = request.getParameter("contact_tel");
			contact_tel_p = request.getParameter("contact_tel_p");
			email = request.getParameter("email");
			
			Member m = new Member(id, password1, password2, name_c, name_a, brith, tel_area1, telephone1, telephone1_p, mobile1, tel_area2, telephone2, telephone2_p, mobile2, address1, address2, original, professional, company_num, company_name, company_job, company_tel_area, company_tel, company_tel_p, contact_name, contact_keep, contact_area, contact_tel, contact_tel_p, email);
				
			    HttpSession session = request.getSession();
				session.setAttribute("m", m);
				
				if (m.validate()){
					//註冊成功 , Forward法
					request.getRequestDispatcher("reg_success.jsp")
					.forward(request, response);
					// 註冊成功, Redirect法
					//response.sendRedirect("reg_success.jsp");
					/*
					 * member <<< reg_success.jsp
					 * add_member.jsp
					 * */
				}else{
					// 註冊失敗
					request.getRequestDispatcher("reg.jsp")
					.forward(request, response);
					//response.sendRedirect("reg.jsp");
					
				}
				
			}
		}
	}
	
	
}
